package com.yang.strategy.util;

import com.yang.common.constant.CacheConstant;
import com.yang.common.constant.RabbitMQConstants;
import com.yang.common.constant.SmsConstant;
import com.yang.common.enums.ExceptionEnums;
import com.yang.common.model.StandardReport;
import com.yang.common.model.StandardSubmit;
import com.yang.strategy.client.BeaconCacheClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yangyongsheng
 * @date 2024/9/8 16:52
 * @description
 */
@Component
public class ErrorSendMsgUtil {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private BeaconCacheClient cacheClient;

    /**
     * 策略模块校验未通过，发送写日志操作
     * @param submit
     */
    public void sendWriteLog(StandardSubmit submit){
        // 封装发送短信失败状态
        submit.setReportState(SmsConstant.REPORT_FAIL);
        // 发送消息到写日志队列
        rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_WRITE_LOG,submit);

    }

    /**
     * 策略模块校验未通过，发送状态报告操作
     * @param submit
     */
    public void sendPushReport(StandardSubmit submit){
        //发送报告的消息前，需要将report对象数据封装
        Integer isCallback = cacheClient.hgetInteger(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), "isCallback");
        // 查看是否需要给客户一个回调
        if (1 == isCallback){
            //如果需要回调，查询客户的回调地址
            String callbackUrl = cacheClient.hgetString(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), "callbackUrl");
            //如果回调地址不为空，开始封装StandardReport
            if (StringUtils.isNotBlank(callbackUrl)){
                StandardReport report = new StandardReport();
                BeanUtils.copyProperties(submit, report);
                report.setIsCallback(isCallback);
                report.setCallbackUrl(callbackUrl);
                // 发送消息到RabbitMQ
                rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_PUSH_REPORT,report);
            }
        }
    }
}
